﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HZ.Supervise.Model;
using HZ.Supervise.Web;
using HZ.Supervise.BLL;
using System.Text;
using HZ.Supervise.Model.Enum;
using HZ.Supervise;
public partial class ProcureMax : BasePage
{
    public ProcureMax()
        {
            this.ModuleNo = Module.趋势走向.ToString();
            this.PageOperateType = OperateType.药品排名.ToString();
        }
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			this.AreaControls1.InitAreaID = CurrentAccount.GetUser().Org.Area.AreaID;

			MaxforMonth1.Banding();
			Databing();
		}
	}
	 
	private void Databing()
	{
        try
        {

		string wherePro = "";

		StringBuilder sb = new StringBuilder();

		if (!string.IsNullOrEmpty(txtproductname.Text)) wherePro += " and A.ProductName like '%" + txtproductname.Text.Trim() + "%'";

		if (!string.IsNullOrEmpty(txtMedicineModel.Text)) wherePro += " and A.MedicineModel like '%" + txtMedicineModel.Text.Trim() + "%'";

        if (!string.IsNullOrEmpty(txtPack.Text)) wherePro += " and A.Pack like '%" + txtPack.Text.Trim() + "%'";

		if (!string.IsNullOrEmpty(txtCompanyname_sc.Text)) wherePro += " and A.CompanyName_SC like '%" + txtCompanyname_sc.Text.Trim() + "%'";
        if (!string.IsNullOrEmpty(txtUnit.Text))
        {
            wherePro += "and A.Unit like'" + txtUnit.Text.Trim() + "'";
        }

		string strAreaId = this.AreaControls1.SelectedAreaID;

		string orderOfrank = "";

		string orderBy = "";

		sb = HZ.Supervise.Web.Function.GetConditionOfAreaId(sb, strAreaId) as StringBuilder;

		if (this.ddlorderby.SelectedValue == "PurchaseAmount" || this.ddlorderby.SelectedValue == "DistributeAmount" || this.ddlorderby.SelectedValue == "WareHouseAmount")
		{

			orderOfrank = " sum(" + this.ddlorderby.SelectedValue + ")";

			orderBy = this.ddlorderby.SelectedValue + "total  " + this.ddlorder.SelectedValue;

		}
		else
		{

			orderOfrank = " AVG(isnull(" + this.ddlorderby.SelectedValue + ",0))";

			orderBy = this.ddlorderby.SelectedValue + " " + this.ddlorder.SelectedValue;

		}

		int totalrecord = 0;

		BLL_Goods bllGoods = new HZ.Supervise.BLL.BLL_Goods();

		DataTable dt;

		dt = bllGoods.GeteGoodsRanking(orderOfrank, MaxforMonth1.Getvalue() + sb, Function.FormatDayTimeByDH("DayTime", MaxforMonth1.Getvalue()) + sb, orderBy, wherePro, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out totalrecord);

		HZ.Supervise.Web.Function.ChangeRate(dt, "DistributeRate", "addDistributeRate");

		HZ.Supervise.Web.Function.ChangeRate(dt, "WareHouseRate", "addWareHouseRate");

		//HZ.Supervise.Web.Function.AddColRank(dt, "pm");

		gvwProcureCatalog2.Visible = false;
		gvwProcureCatalog.Visible = true;
		gvwProcureCatalog.DataSource = dt;
		gvwProcureCatalog.DataBind();

		this.AspNetPager1.RecordCount = totalrecord;

        }
        catch (Exception e)
        {
            e.ToString();

            HZ.Web.WebUtil.Alert("请输入正确的格式！");
        }
	}

	protected void btnQuery_Click(object sender, EventArgs e)
	{
		this.AspNetPager1.CurrentPageIndex = 1;

	}

	protected void lbnSwitch_Click(object sender, EventArgs e)
	{

	}

	protected void btnExcel_Click(object sender, EventArgs e)
	{
		string wherePro = "";

		StringBuilder sb = new StringBuilder();

		if (!string.IsNullOrEmpty(txtproductname.Text)) wherePro += " and A.ProductName like '%" + txtproductname.Text.Trim() + "%'";

		if (!string.IsNullOrEmpty(txtMedicineModel.Text)) wherePro += " and A.MedicineModel like '%" + txtMedicineModel.Text.Trim() + "%'";

        if (!string.IsNullOrEmpty(txtPack.Text)) wherePro += " and A.Pack like '%" + txtPack.Text.Trim() + "%'";

        if (!string.IsNullOrEmpty(txtUnit.Text))
        {
            wherePro += "and A.Unit like'" + txtUnit.Text.Trim() + "'";
        }

		if (!string.IsNullOrEmpty(txtCompanyname_sc.Text)) wherePro += " and A.CompanyName_SC like '%" + txtCompanyname_sc.Text.Trim() + "%'";

		string strAreaId = this.AreaControls1.SelectedAreaID;

		string orderOfrank = "";

		string orderBy = "";

		sb = HZ.Supervise.Web.Function.GetConditionOfAreaId(sb, strAreaId) as StringBuilder;

		if (this.ddlorderby.SelectedValue == "PurchaseAmount" || this.ddlorderby.SelectedValue == "DistributeAmount" || this.ddlorderby.SelectedValue == "WareHouseAmount")
		{

			orderOfrank = " sum(" + this.ddlorderby.SelectedValue + ")";

			orderBy = this.ddlorderby.SelectedValue + "total  " + this.ddlorder.SelectedValue;

		}
		else
		{

			orderOfrank = " AVG(isnull(" + this.ddlorderby.SelectedValue + ",0))";

			orderBy = this.ddlorderby.SelectedValue + " " + this.ddlorder.SelectedValue;

		}

		int totalrecord = 0;

		BLL_Goods bllGoods = new HZ.Supervise.BLL.BLL_Goods();

		DataTable dt;

		dt = bllGoods.GeteGoodsRanking(orderOfrank, MaxforMonth1.Getvalue() + sb, Function.FormatDayTimeByDH("DayTime", MaxforMonth1.Getvalue()) + sb, orderBy, wherePro, 1, 0, out totalrecord);

		HZ.Supervise.Web.Function.ChangeRate(dt, "DistributeRate", "addDistributeRate");

		HZ.Supervise.Web.Function.ChangeRate(dt, "WareHouseRate", "addWareHouseRate");

		HZ.Supervise.Web.Function.AddColRank(dt, "pm");


		HZ.Excel.ExcelHelper.ExportDataForWeb(dt, this.gvwProcureCatalog);

	}

	protected void lbtnSee_Click(object sender, EventArgs e)
	{

	}

	protected void AspNetPager1_PageChanged(object sender, EventArgs e)
	{
		Databing();
	}

	protected void gvwProcureCatalog_PageIndexChanging(object sender, GridViewPageEventArgs e)
	{
		gvwProcureCatalog.PageIndex = e.NewPageIndex;
		gvwProcureCatalog.DataBind();
	}

	/// <summary>
	/// 前台排名格式化
	/// </summary>
	/// <param name="pm"></param>
	/// <returns></returns>
	public string pm(object pm)
	{
		if (int.Parse(pm.ToString()) > 0)
		{
			return pm.ToString();
		}
		else
		{
			return "-";
		}
	}
}

